<?php

namespace app\service;

class OfficeAccount
{
    public function getAuthCode($url)
    {
        $url=urlencode($url);
        $appId=config('wechat.oa_id');
        return "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appId."&redirect_uri={$url}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
    }

    function getUserAccessToken($code){
        $appId=config('wechat.oa_id');
        $appSecret=config('wechat.oa_secret');
        $info=$this->curl_get("https://api.weixin.qq.com/sns/oauth2/access_token?appid={$appId}&secret={$appSecret}&code={$code}&grant_type=authorization_code");
        return json_decode($info,true);
    }

    public function getUserInfo($info){
        $info=$this->curl_get("https://api.weixin.qq.com/sns/userinfo?access_token={$info['access_token']}&openid={$info['openid']}&lang=zh_CN");
        return json_decode($info,true);
    }

    /**
     * @link https://developers.weixin.qq.com/doc/service/guide/h5/jssdk.html#62
     * @return array
     */
    function getSign($url){
        $data['noncestr']=Weixin::randomkeys();
        $data['jsapi_ticket']=$this->getTicket();
        $data['timestamp']=time();
        $data['url']=$url;
        ksort($data);
        $str='';
        $i=0;
        foreach ($data as $key=>$v){
            $i++;
            if($i==1){
                $str .= strtolower($key).'='.$v;
            }else{
                $str .= '&'.strtolower($key).'='.$v;
            }
        }
        $data['signature']=sha1($str);
        return $data;
    }


    function getTicket(){
        $tokenInfo=cache('ticket');
        if (isset($tokenInfo['expire']) && $tokenInfo['expire']>time()){
            return $tokenInfo['ticket'];
        }else{
            $info=$this->curl_get('https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$this->getAccessToken().'&type=jsapi');
            $info=json_decode($info,true);
            if(isset($info['ticket'])){
                cache('ticket',['ticket'=>$info['ticket'],'expire'=>time()+7200]);
                return $info['ticket'];
            }
        }
    }

    function getAccessToken(){
        $tokenInfo=cache('accessToken');
        if (isset($tokenInfo['expire']) && $tokenInfo['expire']>time()){
            return cache('accessToken')['accessToken'];
        }else{
            $appId=config('wechat.oa_id');
            $appSecret=config('wechat.oa_secret');
            $info=$this->curl_get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appId}&secret={$appSecret}");
            $info=json_decode($info,true);
            if(isset($info['access_token'])){
                cache('accessToken',['accessToken'=>$info['access_token'],'expire'=>time()+7200]);
                return $info['access_token'];
            }
        }
    }

    function curl_get($url){
        $curl=curl_init();
        curl_setopt($curl,CURLOPT_URL,$url);
        curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($curl,CURLOPT_HEADER,0);
        curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,0);
        curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,0);
        $info=curl_exec($curl);
        curl_close($curl);
        //$this->info=$info;
        return $info;
    }
    function curl_post($url,$data){
        $curl=curl_init();
        curl_setopt($curl,CURLOPT_URL,$url);
        curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($curl,CURLOPT_HEADER,0);
        curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,0);
        curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,0);
        curl_setopt($curl,CURLOPT_POST,1);
        curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
        $info=curl_exec($curl);
        curl_close($curl);
        //$this->info=$info;
        return $info;
    }


}